package numberporting.server;

import java.util.Date;
import java.util.Iterator;
import java.util.TimerTask;
import numberporting.bean.PortingRequest;
import numberporting.bean.PortingResult;
import numberporting.service.PortingQueue;


public class PortingProcedure extends TimerTask{

    private PortingQueue thePortingQueue;
    
    public PortingProcedure(PortingQueue portingQueue)
    {
        this.thePortingQueue = portingQueue;
    }
    
    public void run() {
        
        Date now = new Date();
        System.out.println("PortingProcedure started");
        
        for(Iterator<PortingRequest> it = this.thePortingQueue.iterator();it.hasNext();)
        {
            PortingRequest tmp = it.next();
            if(tmp.getPorting_date().getTime()<now.getTime())
            {
                System.out.println("PortingRequest for Customer "+tmp.getCustomer().getName()+" sucessfully operated");
                
                PortingResult tmpPortingResult = new PortingResult();
                tmpPortingResult.setNew_provider(tmp.getNew_provider());
                tmpPortingResult.setCustomerId(tmp.getCustomer().getId());
                tmpPortingResult.setCommment("Porting successfully operated");
                tmpPortingResult.setSuccess(true);
                
                tmp.getCustomer().getPortingResults().add(tmpPortingResult); //add PortingResult to customer
                it.remove(); //removes the PortingRequest off the porting queue
            }
        }
        System.out.println("PortingProcedure stopped");
    }

    
}
